package eu.telecom_bretagne.geotablet;

import java.util.Vector;

import org.haptimap.hcimodules.touchovermap.Line;

import android.content.Context;
import android.os.Vibrator;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class TouchData extends Thread {
	
	int numFingers;
	int MAX_FINGERS;
	// Settings
	private boolean vibrationEnabled = true;
	private boolean speechInterruptEnabled = false;
	Vector<Line> lines;
	Context context;
	
	// Speech
	private TextToSpeech tts;
	
	final String LOG_TAG = "TOUCHDATA-THREAD";
	
	public TouchData(int maxfingers, TextToSpeech tts, Context context){
		this.setNumFingers(0);
		this.MAX_FINGERS = maxfingers;
		this.setLines(new Vector<Line>(MAX_FINGERS));
		this.tts = tts;
		this.context = context;
		this.start();
	}
	
	public void run(){
		while(true){			
			//speak and vibrate
			try {
				sleep(100);
				if(numFingers>0){
					sleep(100);
					Log.d( LOG_TAG, "Touch data speak(size) : " + lines.size());
					for (Line l : lines){
						//speakLine(l);
						//vibrate();
					}
				}else{
					stopVibrate();
					stopSpeak();
				}
			} catch (InterruptedException e) {				
				e.printStackTrace();
			}			
		}
	}

	public int getNumFingers() {
		return numFingers;
	}

	public void setNumFingers(int numFingers) {
		this.numFingers = numFingers;
	}

	public Vector<Line> getLines() {
		return lines;
	}

	public void setLines(Vector<Line> lines) {
		this.lines = lines;
	}
	
	public boolean containsLine(Line line){
		for (Line l : lines){
			if (line.getName().equals(l.getName()));
				return true;
		}
		return false;
	}
	
	public synchronized void addLine(int index, Line line){
		if(lines.size()<MAX_FINGERS && !containsLine(line)){
			this.lines.add(index,line);
			++numFingers;
		}
	}
	
	public synchronized void removeLine(int index){
		if(lines.size()>0){
			--numFingers;
			this.lines.remove(index);			
		}
	}

	private void stopVibrate() {
		Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
		if(this.vibrationEnabled) {
			v.cancel();
		}
	}

	private void vibrate() {
		Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
		if(this.vibrationEnabled) {
			// vibrate
			// Start immediately
			// Vibrate for 5000 milliseconds
			// Never sleep
			long[] pattern = { 0, 5000, 0 };
			v.vibrate(pattern,0);
		}
	}

	private void stopSpeak() {
		if(this.tts!=null) {
			this.tts.stop();
		}
	}

	private synchronized void speakLine(Line tl) {
		// speak, if tts was provided
		if(this.tts!=null && ((!this.tts.isSpeaking() || this.speechInterruptEnabled) &&
				(tl.getName()!=null))) {
			String toSpeak = "Unknown";
			if(tl.getName()!=null) {
				toSpeak = tl.getName();
			}
			
			this.tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
		}
	}
	
	/**
	 * Indicates whether vibration on touching a line is enabled.
	 * 
	 * @return true when vibration is enabled, false otherwise.
	 */
	public boolean isVibrationEnabled() {
		return vibrationEnabled;
	}

	/**
	 * Sets whether the device should vibrate when a line is touched.
	 * 
	 * @param vibrationEnabled true to enable vibration, false otherwise
	 */
	public void setVibrationEnabled(boolean vibrationEnabled) {
		this.vibrationEnabled = vibrationEnabled;
	}

	
	/**
	 * Indicates whether speech will be interrupted.
	 * 
	 * @return true when speech interruption is enabled, false otherwise
	 */
	public boolean isSpeechInterruptEnabled() {
		return speechInterruptEnabled;
	}

	/**
	 * Sets whether speech will be interrupted when necessary.
	 * 
	 * @param speechInterruptEnabled true to enable speech interruption, false otherwise
	 */
	public void setSpeechInterruptEnabled(boolean speechInterruptEnabled) {
		this.speechInterruptEnabled = speechInterruptEnabled;
	}
	
	public TextToSpeech getTts() {
		return tts;
	}
	
	public void setTts(TextToSpeech tts) {
		this.tts = tts;
	}
	
}
